package com.cwf.org.controller;

import com.cwf.basic.annotation.AutoScanPermission;
import com.cwf.basic.util.AjaxResult;
import com.cwf.basic.util.PageInfo;
import com.cwf.org.domain.Department;
import com.cwf.org.query.DepartmentQuery;
import com.cwf.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
@AutoScanPermission(name = "部门管理", desc = "部门管理接口")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping
    @ApiOperation(value = "查询所有部门接口")
    @AutoScanPermission(name = "查询所有部门", desc = "查询所有部门接口")
    public AjaxResult queryAll() {
        try {
            return AjaxResult.success(departmentService.queryAll());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("系统故障，请联系管理员");
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询指定部门")
    @AutoScanPermission(name = "查询指定部门", desc = "查询指定部门接口")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        try {
            Department department = departmentService.queryById(id);
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("系统故障，请联系管理员");
        }
    }

    @GetMapping("/tree")
    @ApiOperation(value = "查询部门树")
    @AutoScanPermission(name = "插叙性能部门树", desc = "插叙性能部门树接口")
    public AjaxResult getTree() {
        try {
            List<Department> departments = departmentService.getTree();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("系统故障，请联系管理员");
        }
    }

    @PutMapping
    @ApiOperation(value = "部门新增和修改接口")
    @AutoScanPermission(name = "部门新增和修改", desc = "部门新增和修改接口")
    public AjaxResult insertOrUpdate(@RequestBody Department department) {
        try {
            if (Objects.isNull(department.getId())) {
                departmentService.insert(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除指定部门")
    @AutoScanPermission(name = "删除指定部门", desc = "删除指定部门接口")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            departmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PostMapping
    @ApiOperation(value = "部门分页查询")
    @AutoScanPermission(name = "部门分页查询", desc = "部门分页查询接口")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery) {
        try {
            PageInfo<Department> pageInfo = departmentService.queryPage(departmentQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("系统故障，请联系管理员");
        }
    }

    @PatchMapping
    @ApiOperation(value = "部门批量删除")
    @AutoScanPermission(name = "部门批量删除", desc = "部门批量删除接口")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("系统故障，请联系管理员");
        }
    }
}
